package org.xhs.geomap.net
{
	import com.mapabc.maps.api.basetypes.MLngLat;
	import com.mapabc.maps.api.basetypes.MLngLatBounds;
	
	import flash.events.Event;
	
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class BlockXMLParser
	{
		private var loader:URLLoader;
		private var xml:XML;
		private var blockArray:Array = new Array();
		
		public function BlockXMLParser()
		{
		}
		
		public function parse(url:String):Array
		{
			var urlrequest:URLRequest = new URLRequest(url);
			loader = new URLLoader(urlrequest);
			loader.addEventListener(Event.COMPLETE, loadcomplete);
			return blockArray;
		}
		
		public function loadcomplete(eve:Event):void
		{
			xml = new XML(loader.data);
			for(var i:Number = 0;i < xml.block.length();i++)
			{
				var lnglatsArray:Array = new Array();
				var minLng:Number = 200;
				var maxLng:Number = 0;
				var minLat:Number = 200;
				var maxLat:Number = 0;
				
				for(var j:Number = 0;j < xml.block[i].point.(@type == 0).length();j++)
				{
					var lng:Number = xml.block[i].point[j].@lng;
					var lat:Number = xml.block[i].point[j].@lat;
					if(lng > maxLng)
						maxLng = lng;
					if(lng < minLng)
						minLng = lng;
					if(lat > maxLat)
						maxLat = lat;
					if(lat < minLat)
						minLat = lat;
					var lnglat:MLngLat = new MLngLat(lng, lat);
					lnglatsArray.push(lnglat);
				}
				
				//把第一个点在数组的最后一位再存一次，为绘制区块边界做准备
				lnglatsArray.push(lnglatsArray[0]);  
				var boundsLnglat:MLngLatBounds = new MLngLatBounds(new MLngLat(minLng,minLat), new MLngLat(maxLng, maxLat));
				var blockcode:String = xml.block[i].@id;
				var blockname:String = xml.block[i].@name;
				var centLnglat:MLngLat = new MLngLat(xml.block[i].point.(@type == 1).@lng, xml.block[i].point.(@type == 1).@lat);
				
				var block:BlockDataModel = new BlockDataModel(blockcode, blockname, lnglatsArray, centLnglat, boundsLnglat);
				
				blockArray.push(block);
			}
		}
	}
}